package com.major.mytomcat.servlet;

import com.major.mytomcat.Container;
import com.major.mytomcat.Request;
import com.major.mytomcat.Response;

import java.util.Map;

public class HomeServlet implements Servlet {

    public HomeServlet() {
        init();
    }

    @Override
    public void init() {
        System.out.println("初始化");
    }

    @Override
    public void service(Request request, Response response) {

        String cookie = request.getHeaders().get("cookie");
        System.out.println(cookie);
        if (cookie == null){
            response.write("请你登陆");
        }else {
            System.out.println("this is home");

            String sessionId = cookie.split("=")[1];
            Map<String, Object> session = Container.SESSIONS.get(sessionId);
            session.put("user","zhangsan");

            response.write("<h1>this is home</h1>");
        }
    }

    @Override
    public void destroy() {

    }
}
